<template>
<div class="user-container">
  <div slot="footer" class="footer-container">
    <div class="title-box">用户注册</div>
  </div>
  <el-form :model="addForm" :rules="rules" ref="addForm" label-width="70px">
    <el-form-item label="姓名" prop="nickname">
      <el-input v-model="addForm.nickname" placeholder="请输入姓名" clearable></el-input>
    </el-form-item>
    <el-form-item label="账号" prop="username">
      <el-input v-model="addForm.username" placeholder="请输入账号" clearable></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="addForm.password" placeholder="请输入密码" show-password clearable></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-select v-model="addForm.sex" placeholder="请选择性别" clearable style="width: 100%">
        <el-option v-for="item in genderArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="电话" prop="phone">
      <el-input v-model="addForm.phone" placeholder="请输入电话" clearable></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="footer-container">
    <el-button type="primary" @click="submitForm('addForm')">确定</el-button>
    <el-button @click="cancelForm('addForm')">返回登录</el-button>
  </div>
</div>
</template>

<script>
import {addUser} from "@/api/user";

export default {
  name: "index",
  data() {
    return {
      genderArr: [{'label': '男', 'value': 1}, {'label': '女', 'value': 2}],
      addForm: {
        id: '',
        nickname: '',
        username: '',
        password: '',
        sex: '',
        phone: ''
      },
      rules: {
        nickname: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
        ],
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
        sex: [
          {required: true, message: '请选择性别', trigger: 'change'},
        ],
        phone: [
          {required: true, message: '请输入电话', trigger: 'blur'},
        ],
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {
            nickname: this.addForm.nickname,
            username: this.addForm.username,
            sex: this.addForm.sex,
            phone: this.addForm.phone,
            password: this.addForm.password,
            role: 'user'
          }
          addUser(params).then(response => {
            if(response.code === 200){
              this.$message({
                message: response.msg,
                type: 'success'
              });
            } else {
              this.$message({
                message: response.msg,
                type: 'error'
              });
            }
          })
        } else {
          return false;
        }
      });
    },
    cancelForm(formName) {
      this.$refs[formName].resetFields();
      this.$router.push('/login')
    },
  }
}
</script>

<style scoped>
.user-container {
  margin: 30px;
}
.title-box {
  margin: 20px;
}
.footer-container {
  text-align: center;
}
</style>
